package com.xianzhiOA.util;

import com.xianzhiOA.bean.XzPerson;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xwpf.usermodel.*;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/3/31.
 */
public class ImportUtil {
    //word~!
    public static final XzPerson importForZL (File file,XzPerson person) throws Exception {

        FileInputStream fis = new FileInputStream(file);
        XWPFDocument doc = new XWPFDocument(fis);
        //所有表格
        List<XWPFTable> tables = doc.getTables();
        //取得第一个表格
        XWPFTable tb0 = tables.get(0);
        XWPFTableCell ypzw = tb0.getRow(0).getCell(1);
        System.out.println(ypzw.getText());
        //应聘职位set
        person.setPersonJob(ypzw.getText());
        //取得第二个表格
        XWPFTable tb = tables.get(1);
        //取得姓名行
        XWPFTableRow nameRow = tb.getRow(0);
        XWPFTableCell nameCell = nameRow.getCell(0);
        nameCell.getPartType();
        List<XWPFTable> nameTables =  nameCell.getTables();
        List<XWPFTableRow> rows2 = nameTables.get(0).getRows();
        //姓名
        XWPFTableCell nameCell1 = rows2.get(0).getCell(0);
        String name =  nameCell1.getText();
        System.out.println(name);
        //姓名set
        person.setPersonName(name);
        //取得信息行
        XWPFTableRow infoRow = tb.getRow(1);
        XWPFTableCell infoCell = infoRow.getCell(0);
        List<XWPFParagraph> ps = infoCell.getParagraphs();
        String ybInfo = ps.get(0).getText();
        String[] ss =  ybInfo.split("    ");
        //性别
        String sex = ss[0];
        person.setPersonSex(sex);
        System.out.println(sex);
        //生日
        String birthDay =  ss[1].substring(ss[1].indexOf("(")+1,ss[1].indexOf(")"));
        System.out.println(birthDay);
        person.setPersonBirth(birthDay);
        //工作年限
        String gznx = ss[2];
        System.out.println(gznx);
        if(gznx.endsWith("经验")){
            //年限存入
            person.setPersonExp(gznx);
            //学历
            String xl = ss[3];
            System.out.println(xl);
            person.setPersonEducation(xl);
        }else{
            //学历
            String xl = ss[2];
            System.out.println(xl);
            person.setPersonEducation(xl);
        }

        return  person;
    }
    public static final XzPerson importForQC(File file,XzPerson person) throws Exception {
        FileInputStream fis = new FileInputStream(file);
        XWPFDocument doc = new XWPFDocument(fis);
        //所有表格
        List<XWPFTable> tables = doc.getTables();
        List<XWPFTableRow> rows;
        List<XWPFTableCell> cells;
        //内容表格
        XWPFTable tab = tables.get(1);
        XWPFTableCell celli = tab.getRow(0).getCell(0);
        List<XWPFTable> tabs =   celli.getTables();
        XWPFTable tt = tabs.get(0);
        XWPFTableCell cellj = tt.getRow(0).getCell(0);
        List<XWPFTable> tts =   cellj.getTables();

        //姓名电话栏
        XWPFTable t1 = tts.get(1);
        XWPFTableCell rInfo = t1.getRow(0).getCell(1);
        List<XWPFTable> ttt = rInfo.getTables();
        XWPFTable t11 = ttt.get(0);
        //名字
        XWPFTableCell nameCell = t11.getRow(0).getCell(0);
        String nameStr = nameCell.getText();
        String name = nameStr.substring(1,nameStr.indexOf("流程状态"));
        System.out.println(name);
        person.setPersonName(name);
        //电话
        XWPFTableCell phoneCell = t11.getRow(1).getCell(1);
        XWPFTable phoneTable = phoneCell.getTables().get(0);
        XWPFTableCell phoneCell1 = phoneTable.getRow(0).getCell(1);
        String tel = phoneCell1.getText();
        System.out.println(tel);
        person.setPersonTel(tel);
        //基本信息
        XWPFTableCell infoCell = t11.getRow(2).getCell(0);
        String infos = infoCell.getText();
        String [] ins = infos.split(" | ");
        //性别
        String sex = ins[0].substring(1);
        System.out.println(sex);
        person.setPersonSex(sex);
        //生日
        String birthDay = ins[2].substring(4,ins[2].indexOf("月")+1);
        System.out.println(birthDay);
        person.setPersonBirth(birthDay);
        //工作年限
        String gznx = ins[7];
        System.out.println(gznx);
        person.setPersonExp(gznx);
        //工作、学历信息
        XWPFTable t2 = tts.get(2);
        XWPFTableCell gxInfo = t2.getRow(0).getCell(0);
        XWPFTable gxtable = gxInfo.getTables().get(0);
        XWPFTableCell xlInfo = gxtable.getRow(0).getCell(1);
        XWPFTable xlTable = xlInfo.getTables().get(0);
        //专业
        String zy =  xlTable.getRow(1).getCell(1).getText();
        System.out.println(zy);
        //学校
        String xx =  xlTable.getRow(2).getCell(1).getText();
        System.out.println(xx);
        //学历
        String xl =  xlTable.getRow(3).getCell(1).getText();
        System.out.println(xl);
        person.setPersonEducation(xl);
        //应聘职位
        XWPFTable t0 = tables.get(0);
        XWPFTableCell qzCell = t0.getRow(0).getCell(0);
        String qzStr = qzCell.getText();
        String qzS1 = qzStr.substring(6,qzStr.indexOf("应聘公司"));
        qzS1 = replaceBlank(qzS1);
        System.out.println(qzS1);
        person.setPersonJob(qzS1);

        return  person;
    }
    //excel
    public static List<XzPerson> importAllForZL(List<File> files) throws Exception{
        //人员集合
        List<XzPerson> persons = new ArrayList<>();
        for (File file : files) {
            InputStream is = new FileInputStream(file);
            Workbook workBook = WorkbookFactory.create(is);
            int ss = workBook.getNumberOfSheets();
            System.out.println(ss);
            List<String> names = new ArrayList<>();
            for (int i = 1; i < ss; i++) {
               Sheet sheet = workBook.getSheetAt(i);
               String name = sheet.getRow(1).getCell(1).toString();
               names.add(name);
            }
            Sheet sheet = workBook.getSheetAt(0);
            int rows = sheet.getPhysicalNumberOfRows();
            for (int i = 1; i < rows; i++) {
                XzPerson person = new XzPerson();
                //姓名
                String name = names.get(i-1);
                System.out.println(name);
                person.setPersonName(name);
                //职位
                String zw = sheet.getRow(i).getCell(2).toString();
                System.out.println(zw);
                person.setPersonJob(zw);
                //性别
                String sex = sheet.getRow(i).getCell(4).toString();
                System.out.println(sex);
                person.setPersonSex(sex);
                //生日
                String birth = sheet.getRow(i).getCell(5).toString();
                birth = birth.replace("/","-");
                System.out.println(birth);
                person.setPersonBirth(birth);
                //工作年限
                String gznx =  sheet.getRow(i).getCell(6).toString();
                System.out.println(gznx+"年工作经验");
                person.setPersonExp(gznx+"年工作经验");
                //学校
                String xx =  sheet.getRow(i).getCell(13).toString();
                System.out.println(xx);
                //专业
                String zy = sheet.getRow(i).getCell(14).toString();
                System.out.println(zy);
                //学历
                String xl = sheet.getRow(i).getCell(15).toString();
                System.out.println(xl);
                person.setPersonEducation(xl);
                //电话
                String phone = sheet.getRow(i).getCell(7).toString();
                System.out.println(phone);
                person.setPersonTel(phone);
                persons.add(person);
            }
        }
        return persons;
    }

    public static List<XzPerson> importAllForQC(List<File> files) throws Exception{
        //人员集合
        List<XzPerson> persons = new ArrayList<>();
        for (File file : files) {
            InputStream is = new FileInputStream(file);
            Workbook workBook = WorkbookFactory.create(is);
            Sheet sheet = workBook.getSheetAt(0);
            int rows = sheet.getPhysicalNumberOfRows();
            for (int i = 1; i < rows; i++) {
                XzPerson person = new XzPerson();
                //姓名
                String name = sheet.getRow(i).getCell(0).toString();
                System.out.println(name);
                person.setPersonName(name);
                //职位
                String zw = sheet.getRow(i).getCell(3).toString();
                System.out.println(zw);
                person.setPersonJob(zw);
                //性别
                String sex = sheet.getRow(i).getCell(7).toString();
                System.out.println(sex);
                person.setPersonSex(sex);
                //生日
                String birth = sheet.getRow(i).getCell(8).toString();
                System.out.println(birth);
                person.setPersonBirth(birth);
                //工作年限
                String gznx =  sheet.getRow(i).getCell(11).toString();
                System.out.println(gznx+"工作经验");
                person.setPersonExp(gznx+"工作经验");
                //学校
                String xx =  sheet.getRow(i).getCell(13).toString();
                System.out.println(xx);
                //专业
                String zy = sheet.getRow(i).getCell(14).toString();
                System.out.println(zy);
                //学历
                String xl = sheet.getRow(i).getCell(12).toString();
                System.out.println(xl);
                person.setPersonEducation(xl);
                //电话
                String phone = sheet.getRow(i).getCell(15).toString();
                System.out.println(phone);
                person.setPersonTel(phone);
                persons.add(person);
            }
        }
        return persons;
    }


    public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
}
